var ACTION = require("../base/action");
var utils = require("../newUtils/utils");
var ocrGml = require("../newUtils/ocrGml");
var readAction = new ACTION("阅读");
readAction.exec = function () {
  readFirst();
  return;
  var randomNumber = utils.getRandom(1, 100);
  if (randomNumber < 70) {
    readFirst();
    return;
  }
  if (randomNumber < 90) {
    return;
  }
  if (randomNumber < 100) {
    return;
  }
};

function readFirst() {
  var content = ["小时前", "天前", "周前", "前天", "月前"];
  var ocrItem = ocrGml.findTextOne(
    content,
    false,
    null,
    5,
    (device.height / 10) * 2
  );
  if (ocrItem) {
    console.log("找到-" + ocrItem.text + "-");
    utils.clickOcrItem(ocrItem);
    sleep(utils.getRandom(1.5, 2, 1000));
    var ocrItem = ocrGml.captureExistOne(["全屏"]);
    if (ocrItem) {
      console.log("找到-" + ocrItem.text + "-,判断是视频");
      sleep(utils.getRandom(30, 32, 1000));
      return;
    }
    var span = utils.getRandom(30, 32, 1);
    console.log("没找到-全屏-开始看文章-" + span + "-s");
    readArticle(span);
    return;
  }
}

function swipeUp() {
  utils.swipeX(
    ((device.width / 10) * 5 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 10) * 5 * utils.getRandom(90, 100, 1)) / 100,
    ((device.width / 10) * 5 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 10) * 2 * utils.getRandom(90, 100, 1)) / 100,
    utils.getRandom(0.5, 1.2, 1000)
  );
}
function swipeDown() {
  utils.swipeX(
    ((device.width / 10) * 5 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 10) * 3 * utils.getRandom(90, 100, 1)) / 100,
    ((device.width / 10) * 5 * utils.getRandom(90, 100, 1)) / 100,
    ((device.height / 10) * 9 * utils.getRandom(90, 100, 1)) / 100,
    utils.getRandom(0.5, 1.2, 1000)
  );
}

function readArticle(span) {
  var swipeTimes = 0;
  var startTime = Date.now();
  while (true) {
    var randomNumber = utils.getRandom(0, 100);
    if (swipeTimes < 5) {
      console.log("向上滑动一次");
      swipeUp();
    } else {
      if (randomNumber > 90) {
        console.log("向下滑动一次");
        swipeDown();
      } else {
        console.log("向上滑动一次");
        swipeUp();
      }
    }
    var watchContentSpan = utils.getRandom(1.5, 3, 1000);
    sleep(watchContentSpan);
    console.log("已看内容-" + watchContentSpan / 1000 + "-s");
    swipeTimes = swipeTimes + 1;
    var endTime = Date.now();
    if (endTime - startTime >= span * 1000) {
      break;
    }
  }
}

module.exports = function () {
  readAction.run();
};
